package com.lzb.musicplayer.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzb.musicplayer.BaseApplication;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.bean.MusicInfo;

import java.util.ArrayList;
import java.util.List;

public class SpUtils {



    private static SpUtils mSpUtils;

    private static int MODE = Context.MODE_PRIVATE;

    public static final String NAME = "USER";
    public static String MOBETYPE="mobeType";
    public static String TIMETYPE="timeType";

    public static SpUtils getInstance() {
        if (mSpUtils == null) {
            synchronized (SpUtils.class) {
                if (mSpUtils == null) {
                    mSpUtils = new SpUtils();
                }
            }
        }
        return mSpUtils;
    }



    public void put(String key, Object object) {
        if (object instanceof String) {
            putString(key, (String) object);
        } else if (object instanceof Integer) {
            putInt(key, (Integer) object);
        }
    }

    public  void putInt(String key, int value) {
        putInt(NAME, key, value);
    }

    public  void putString(String key, String value) {
        putString(NAME, key, value);
    }

    public  void putString(String shareName, String key, String value) {
        SharedPreferences share = getSharePrefer(shareName);
        SharedPreferences.Editor editor = share.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public  void putInt(String shareName, String key, int value) {
        SharedPreferences share = getSharePrefer(shareName);
        SharedPreferences.Editor editor = share.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public  void putLong(String key, long value) {
        putLong(NAME, key, value);
    }
    public  void putLong(String shareName, String key, long value) {
        SharedPreferences share = getSharePrefer(shareName);
        SharedPreferences.Editor editor = share.edit();
        editor.putLong(key, value);
        editor.commit();
    }
    public  void putFloat(String key, float value) {
        putFloat(NAME, key, value);
    }
    public  void putFloat(String shareName, String key, float value) {
        SharedPreferences share = getSharePrefer(shareName);
        SharedPreferences.Editor editor = share.edit();
        editor.putFloat(key, value);
        editor.commit();
    }
    public  float getFloat(String key, float defValue) {
        return getFloat(NAME, key, defValue);
    }
    public  float getFloat(String shareName, String key, float defValue) {
        SharedPreferences share = getSharePrefer(shareName);
        return share.getFloat(key, defValue);
    }
    public  int getInt(String key, int defValue) {
        return getInt(NAME, key, defValue);
    }
    public  Long getLong(String key, long defValue) {
        return getLong(NAME, key, defValue);
    }
    public  Long getLong(String shareName, String key, long defValue) {
        SharedPreferences share = getSharePrefer(shareName);
        return share.getLong(key, defValue);
    }
    public  int getInt(String shareName, String key, int defValue) {
        SharedPreferences share = getSharePrefer(shareName);
        return share.getInt(key, defValue);
    }

    public  String getString(String key) {
        return getString(NAME, key, "");
    }
    public  String getString(String key,String strDefault) {
        return getString(NAME, key, strDefault);
    }

    public  String getString(String shareName, String key, String defValue) {
        SharedPreferences share = getSharePrefer(shareName);
        return share.getString(key, defValue);
    }


    private  SharedPreferences getSharePrefer(String shareName) {
        return BaseApplication.context.getSharedPreferences(shareName, MODE);
    }

    public  void putList(String key, List<MusicFile> datalist) {
        Gson gson = new Gson();
        //转换成json数据，再保存
        SharedPreferences share = getSharePrefer(NAME);
        SharedPreferences.Editor editor = share.edit();
        String Json = gson.toJson(datalist);
        editor.putString(key, Json);
        editor.commit();
    }

    public  List<MusicFile> getList(String key) {
        List<MusicFile> datalist;
        SharedPreferences share = getSharePrefer(NAME);
        String Json = share.getString(key, null);
        if (null == Json) {
            return new ArrayList<MusicFile>();
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(Json, new TypeToken<List<MusicFile>>() {
        }.getType());
        return datalist;

    }
    public  List<MusicInfo> getListBc(String key) {
        List<MusicInfo> datalist;
        SharedPreferences share = getSharePrefer(NAME);
        String Json = share.getString(key, null);
        if (null == Json) {
            return new ArrayList<MusicInfo>();
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(Json, new TypeToken<List<MusicInfo>>() {
        }.getType());
        return datalist;

    }
    public  void putListBc(String key, List<MusicInfo> datalist) {
        Gson gson = new Gson();
        //转换成json数据，再保存
        SharedPreferences share = getSharePrefer(NAME);
        SharedPreferences.Editor editor = share.edit();
        String Json = gson.toJson(datalist);
        editor.putString(key, Json);
        editor.commit();
    }
}

